package ecom.beans.dao;

import ecom.beans.UserBean;
import ecom.beans.exception.UserBeanException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class UserDAO extends DAO {

    public static Long saveUser(UserBean user) throws UserBeanException {
        String exception = "";
        boolean isException = false;

        if (getByPseudo(user.getPseudo()) != null) {
            exception = "This pseudo already exist";
            isException = true;
        }
        if (getByEmail(user.getEmail()) != null) {
            isException = true;
            if (exception.equals("")) {
                exception = "This email already exist";
            } else {
                exception = exception + ", this email already exist";
            }
        }
        if (user.getPseudo().length() > 50) {
            isException = true;
            if (exception.equals("")) {
                exception = "This pseudo should not exceed 50 characters";
            } else {
                exception = exception + ", this pseudo should not exceed 50 characters";
            }
        }
        if (user.getPassword().length() > 50) {
            isException = true;
            if (exception.equals("")) {
                exception = "This password should not exceed 50 characters";
            } else {
                exception = exception + ", this password should not exceed 50 characters";
            }
        }
        if (user.getEmail().length() > 50) {
            isException = true;
            if (exception.equals("")) {
                exception = "This email should not exceed 50 characters";
            } else {
                exception = exception + ", this email should not exceed 50 characters";
            }
        }
        if (user.getAdress() != null) {
            if (user.getAdress().length() > 100) {
                isException = true;
                if (exception.equals("")) {
                    exception = "This adress should not exceed 100 characters";
                } else {
                    exception = exception + ", this adress should not exceed 100 characters";
                }
            }
        }
        if (user.getPostalCode() != null) {
            if (user.getPostalCode().length() > 10) {
                isException = true;
                if (exception.equals("")) {
                    exception = "This postalcode should not exceed 10 characters";
                } else {
                    exception = exception + ", this postalcode should not exceed 100 characters";
                }
            }
        }
        if (user.getCity() != null) {
            if (user.getCity().length() > 100) {
                isException = true;
                if (exception.equals("")) {
                    exception = "This city should not exceed 100 characters";
                } else {
                    exception = exception + ", this city should not exceed 100 characters";
                }
            }
        }
        if (user.getCity() != null) {
            if (user.getCity().length() > 100) {
                isException = true;
                if (exception.equals("")) {
                    exception = "This city should not exceed 100 characters";
                } else {
                    exception = exception + ", this city should not exceed 100 characters";
                }
            }
        }
        if (user.getCountry() != null) {
            if (user.getCountry().length() > 50) {
                isException = true;
                if (exception.equals("")) {
                    exception = "This country should not exceed 50 characters";
                } else {
                    exception = exception + ", this city should not exceed 50 characters";
                }
            }
        }
        if (user.getCurrency() != null) {
            if (user.getCurrency().length() > 3) {
                isException = true;
                if (exception.equals("")) {
                    exception = "This phone number should not exceed 3 characters";
                } else {
                    exception = exception + ", this phone number should not exceed 3 characters";
                }
            }
        }

        if (AccountDAO.getByIBAN(user.getAccount().getIban()) != null) {
            isException = true;
            if (exception.equals("")) {
                exception = "This iban already exist, you couldn't have the same account with two users";
            } else {
                exception = exception + ", this iban already exist, you couldn't have the same account with two users";
            }
        }
        if (isException) {
            throw new UserBeanException(exception);
        }

        saveOrUpdate(user);
        return user.getIdUser();
    }

    public static void deleteUser(UserBean user) {
        delete(user);
    }

    public static void updateUser(UserBean user) {
        saveOrUpdate(user);
    }

    public static Set<UserBean> getAllUsers(String order) {
        return fetchAll("UserBean.findAll", null, order);
    }

    public static UserBean getByPseudoAndPassword(String pseudo, String password) {
        List parameters = new ArrayList();
        parameters.add("pseudo");
        parameters.add(pseudo);
        parameters.add("password");
        parameters.add(password);
        return (UserBean) fetchOne("UserBean.findByPseudoAndPassword", parameters);
    }

    public static UserBean getByPseudo(String pseudo) {
        List parameters = new ArrayList();
        parameters.add("pseudo");
        parameters.add(pseudo);
        return (UserBean) fetchOne("UserBean.findByPseudo", parameters);
    }

    public static UserBean getById(Long idUser) {
        List parameters = new ArrayList();
        parameters.add("idUser");
        parameters.add(idUser);
        return (UserBean) fetchOne("UserBean.findByIdUser", parameters);
    }

    public static UserBean getByEmail(String email) {
        List parameters = new ArrayList();
        parameters.add("email");
        parameters.add(email);
        return (UserBean) fetchOne("UserBean.findByEmail", parameters);
    }
}
